<template>
  <div class="my-course goods-list">
    <div class="mescroll"  ref="mescroll">
      <tab class="list-tab" 
          custom-bar-width="1.7rem"
          bar-active-color="#172F6D"
          >
        <tab-item selected @on-item-click="onItemClick('all')">全部分类</tab-item>
        <tab-item @on-item-click="onItemClick(0)">音频</tab-item>
        <tab-item @on-item-click="onItemClick(1)">视频</tab-item>
      </tab>
      <div class="item-list" v-if="lists && lists.length > 0">
        <div class="item" v-for="(item,index) in lists" :key="index" @click="router({path: './courseDetails', query: {id: item.id}})">
          <img :src="item.img" alt="">
          <i class="icon audio" v-if="item.audio_type==0">音频</i>
          <i class="icon" v-else-if="item.audio_type == 1">视频</i>
          <i class="icon" v-else></i>
          <div class="cons">
            <p class="title">「课程」 {{item.title}}</p>
            <p class="info">{{item.num}}人已购买</p>
            <span class="price" v-if="item.pay_type==0">免费</span>
            <span class="price" v-else>¥{{item.price}}</span>
          </div>
        </div>
      </div>
      <div class="none" v-else>
        <img src="@/assets/sousuomendian04.png" alt="">
        <p>暂无相关内容</p>
      </div>
    </div>
  </div>
</template>

<script>
  import {coursecollect} from '@/api/api'
  import MeScroll from 'mescroll.js'
  import 'mescroll.js/mescroll.min.css'
  import { Tab, TabItem } from 'vux'

  export default {
    components: {
      Tab,
      TabItem,
    },
    data () {
      return {
        lists:[],
        audio:'all',
      }
    },
    created () {
    },
    mounted () {
      this.mescroll = new MeScroll(this.$refs.mescroll, { 
        up: {
          callback: this.upCallback
        }
      })
    },
    methods: {
      router (path) {
        this.$router.push(path)
      },
      upCallback (page) {
        coursecollect(this.audio,page.num, page.size).then((res) => {
          console.log(res)
          if (page.num == 1){
            this.lists = []
          }
          if(res.data == null){
            console.log(1)
            this.mescroll.endErr()
            return
          }
          this.$nextTick(() => {
            this.mescroll.endSuccess(res.data.data.list.length);
          })
          this.lists = this.lists.concat(res.data.data.list)
        }).catch((err) => {
          console.dir(err)
          this.mescroll.endErr()
        })
      },
      onItemClick (index) {
        this.lists=[]
        this.audio=index
        this.mescroll.resetUpScroll()
      },
    }
  }
</script>

<style lang="stylus" scoped>
.my-course
  width 100%
  height 100vh
  overflow hidden
  background-color #fff
  .list-tab
    padding-top 5rem
    .vux-tab-container
      height 5rem
      .vux-tab-item.vux-tab-selected
        font-weight bold
      .vux-tab-item
        color #000
        font-size 1.6rem
        font-weight 400
  .mescroll
    position fixed
    top 0
    bottom 0
    height auto
    width 100vw
    overflow-x hidden
    .top-img
      float left
    .search
      float left
      width 92%
      margin-left 4%
      text-align center
      background-color #fff
      height 4.5rem
      line-height 4.5rem
      margin-top -2.25rem
      margin-bottom 1rem
      img
        display inline-block
        vertical-align middle
        width 1.6rem
      span
        display inline-block
        vertical-align middle
        font-size 1.3rem
        color #172F6D
        margin-left .5rem
  .item-list
    margin-top .5rem
    padding 0 1.5rem
    .item
      background-color #fff
      padding 1.5rem 0
      display flex
      border-bottom 1px solid #e6e6e6
      position relative
      .icon
        position absolute
        width 4rem
        height 1.6rem
        line-height 1.6rem
        text-align center
        color #fff
        font-size 1.1rem
        background-color rgba(0,0,0,.6)
        border-radius .2rem
        left 7.8rem
        top 6.5rem
      .icon.audio
        background-color rgba(168,30,39,.8)
      img
        width 12.5rem
        height 7rem
        max-width 12.5rem
        object-fit cover
      .cons
        width calc(100% - 14rem)
        margin-left 1rem
        .title
          overflow : hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2; 
          -webkit-box-orient: vertical;
          font-size 1.3rem
          color #333
          font-weight 500
          position relative
          left -.8rem
        .info
          color #999
          font-size 1.1rem
          margin-top 1rem
          font-weight 500
        .price
          margin-top 1rem
          font-size 1.3rem
          color #D03718
          font-weight bold
  .none
    img
      position absolute
      top 20.3rem
      width 7.25rem
      left 50%
      margin-left -3.625rem
    p
      color #666
      font-size 1.3rem
      text-align center
      position absolute
      width 100%
      top 32rem
</style>
